package cn.gailvlun.gll.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.util.LogUtil;



public class MatchLoadingView extends View {
    public MatchLoadingView(Context context) {
        this(context, null);
    }

    public MatchLoadingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MatchLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.MatchLoadingView);
        mDotSize = ta.getDimensionPixelSize(R.styleable.MatchLoadingView_dotSize, dp2px(6));
        mDotColor = ta.getColor(R.styleable.MatchLoadingView_dotColor, getResources().getColor(R.color.white));
        mLineColor = ta.getColor(R.styleable.MatchLoadingView_lineColor, getResources().getColor(R.color.white));
        mIsDash = ta.getBoolean(R.styleable.MatchLoadingView_isDash, true);
        ta.recycle();

        initPaint();
        initCircle();

        va = ValueAnimator.ofFloat(-90, 270);
        va.setInterpolator(new LinearInterpolator());
        va.setRepeatMode(ValueAnimator.RESTART);
        va.setRepeatCount(ValueAnimator.INFINITE);
        va.setDuration(2000);
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                endDegree = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        va.start();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        va.cancel();
    }

    private ValueAnimator va;

    private Paint mPaint;
    private Paint dashPaint;
    private RectF circleF;

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);

        dashPaint = new Paint();
        dashPaint.setAntiAlias(true);
        dashPaint.setPathEffect(new DashPathEffect(new float[] {dp2px(2), dp2px(2)}, 0));
    }

    private void initCircle() {
        circleF = new RectF();
    }

    private int mDotSize;
    private int mDotColor;
    private int mLineColor;
    private boolean mIsDash;

    private float startDegree = -90;
    private float endDegree = -90;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        
        mPaint.setColor(mDotColor);
        mPaint.setStyle(Paint.Style.FILL);

        canvas.translate(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
        canvas.drawCircle(0, -getMeasuredHeight() / 2 + mDotSize / 2, mDotSize / 2, mPaint);

        
        canvas.drawCircle((float) Math.cos(Math.toRadians(endDegree)) * (getMeasuredWidth() / 2 - mDotSize / 2),
                (float) Math.sin(Math.toRadians(endDegree)) * (getMeasuredWidth() / 2 - mDotSize / 2),
                mDotSize / 2, mPaint);

        
        if (mIsDash) {
            dashPaint.setColor(mLineColor);
            dashPaint.setAlpha(127);
            dashPaint.setStyle(Paint.Style.STROKE);
            dashPaint.setStrokeWidth(dp2px(1));

            canvas.drawCircle(0, 0, getMeasuredWidth() / 2 - mDotSize / 2, dashPaint);
        }

        
        mPaint.setColor(mLineColor);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(dp2px(1));

        circleF.set(-getMeasuredWidth() / 2 + mDotSize / 2,
                -getMeasuredHeight() / 2 + mDotSize / 2,
                getMeasuredWidth() / 2 - mDotSize / 2,
                getHeight() / 2 - mDotSize / 2);
        canvas.drawArc(circleF,
                startDegree, endDegree - startDegree, false, mPaint);
    }

    private int dp2px(float dp) {
        return (int) (getContext().getResources().getDisplayMetrics().density * dp + 0.5f);
    }
}
